/**
 *
 */
package com.cht.sys;

import java.net.MalformedURLException;
//import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.Base64;

import com.cht.sys.to.LDAPResult;

/**
 * @author Administrator
 *
 */
public class LDAPWSUtil {

	/**
	 *
	 */
	public LDAPWSUtil() {
	}

	private static Call initial(String wsURL) throws ServiceException, MalformedURLException {

		String endpoint = wsURL;

		Service service = new Service();

		Call call = (Call) service.createCall();

		call.setTargetEndpointAddress(new java.net.URL(endpoint));

		return call;
	}

	private static LDAPResult checkEmpNOAndPwd(String wsURL, String callerID, String userID, String passwd) {
		Object[] params = null;
		byte[] secretKey = { 90, 20, 103, 44, -40, -102, -5, 51, -54, -49, 89, 111, -88, 66, -52, 60 };
		LDAPResult result = new LDAPResult();

		//System.out.println(new Date().toString() + ": checkEmpNOAndPwd start");
		if (wsURL == null || wsURL.trim().length() == 0 || callerID == null || callerID.trim().length() == 0 || userID == null || userID.trim().length() == 0
				|| passwd == null || passwd.trim().length() == 0) {
			result.setResultCode(LDAPResult.LDAP_BUSY);
			result.setResultDesc("one of the arguments is invalid");
			return result;
		}

		try {
			//System.out.println(new Date().toString() + ": emcrypt start");
			SecretKeySpec spec = new SecretKeySpec(secretKey, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, spec);
			byte[] encryptedPWD = cipher.doFinal(passwd.getBytes());
			String base64PWD = Base64.encode(encryptedPWD);

			//System.out.println(new Date().toString() + ": web service initial");
			Call call = initial(wsURL);
			//System.out.println(new Date().toString() + ": web service access");
			call.setOperationName("checkEmpNOAndPwd");
			params = new Object[] { callerID, userID, base64PWD };

			String ret = (String)call.invoke(params);

			if (ret.charAt(0) != '0') {
				String[] retArray = ret.split("|");

				if (retArray.length == 1) {
					result.setResultCode(Integer.parseInt(retArray[0]));
				} else if (retArray.length == 2) {
					result.setResultCode(Integer.parseInt(retArray[0]));
					result.setResultDesc(retArray[1]);
				} else {
					result.setResultCode(LDAPResult.LDAP_BUSY);
				}
			}
			//System.out.println(new Date().toString() + ": web service return");

			return result;
		} catch (Exception e) {
			result.setResultCode(LDAPResult.LDAP_BUSY);
			result.setResultDesc(e.toString());
			return result;
		}
	}

	public static void main(String[] args) {
		if (args.length == 4) {
			//System.out.println(new Date().toString() + ": main start");
			LDAPResult result = checkEmpNOAndPwd(args[0], args[1], args[2], args[3]);
			System.out.print(result.getResultCode());
		} else {
			System.out.println("4 arguements needed: wsURL, callerID, employeeno, password");
		}
	}
}
